using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using System.Globalization;
using System.Threading;
using System.Reflection;

namespace EntLibContrib.Validation.Design.UI
{
    public partial class TestValidatorUI : Form
    {
        CultureInfoConverter ciConverter = new CultureInfoConverter();
        ValidatorData validatorData;
        Type targetType;

        public TestValidatorUI(ValidatorData validatorData)
        {
            this.validatorData = validatorData;
            targetType = typeof(string);
            InitializeComponent();

            Text = String.Format(Text, validatorData.Name);

            foreach (CultureInfo StdValue in ciConverter.GetStandardValues())
            {
                if (!StdValue.IsNeutralCulture)
                {
                    comboBox2.Items.Add(ciConverter.ConvertToString(StdValue));
                }
            }
            comboBox2.SelectedItem = ciConverter.ConvertToString(CultureInfo.InvariantCulture);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            TestValidator();
        }

        private void TestValidator()
        {
            try
            {
                object valueToTest = TypeDescriptor.GetConverter(targetType).ConvertFrom(null, ciConverter.ConvertFrom(comboBox2.Text) as CultureInfo, textBox1.Text);
                //OUCH!
                Validator validator = (Validator)validatorData.GetType().GetMethod("DoCreateValidator", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance, null, new Type[] { typeof(Type) }, new ParameterModifier[0]).Invoke(validatorData, new object[] { targetType });
                List<ValidationResult> result = new List<ValidationResult>(validator.Validate(valueToTest));
                if (result.Count > 0)
                {
                    label2.Text = "Value is invalid: " + ((result[0].Message.Length == 0) ? "<empty>" : result[0].Message);
                }
                else
                {
                    label2.Text = "Value is valid";
                }
            }
            catch(Exception ex)
            {
                label2.Text = ex.Message;
            }

            toolTip1.SetToolTip(label2, label2.Text);
        }

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            targetType = Type.GetType(comboBox1.Text, false);
            if (targetType != null)
            {
                comboBox1.ForeColor = Color.Black;

                TestValidator();
            }
            else
            {
                comboBox1.ForeColor = Color.Red;
            }
        }

        private void comboBox2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                ciConverter.ConvertFromString(comboBox2.Text);
                comboBox2.ForeColor = Color.Black;
                TestValidator();

            }
            catch
            {
                comboBox2.ForeColor = Color.Red;
            }
        }
    }
}